import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/Login/Login'
import Home from '../views/Home/Home'
import Upload from '../views/Home/Upload/Upload'
import Images from '../views/Home/Images/Images'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/upload',
    children: [
      {
        path: '/upload',
        name: 'Upload',
        component: Upload,
      },
      {
        path: '/image',
        name: 'Images',
        component: Images
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

export default router

// 挂载一个导航守卫
router.beforeEach( (to,from,next) => {

  console.log(to.path);
  // 放行 login
  if (to.path === '/login') return next();
  // 取token值
  const token = window.sessionStorage.getItem("token");
  console.log(token);
  // 如果token为null 强行跳转到登录
  if (!token) {
    return next('/login')
    console.log("token空了");
  };
  // token不为空 放行
  return next()

})
